package com.bjx.flow.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.flow.domain.entity.FlowDesignEntity;
import com.bjx.flow.domain.query.FlowDesignQuery;
import com.bjx.flow.domain.vo.FlowDesignDeployVO;
import com.bjx.flow.domain.vo.FlowDesignVO;
import com.bjx.flow.mapper.FlowDesignMapper;
import com.bjx.flow.mmx.domain.entity.MmxFlowDefinitionEntity;
import com.bjx.flow.mmx.service.MmxFlowDefinitionService;
import com.bjx.flow.service.IFlowDesignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @desc: 流程设计服务实现
 * @author: liushuai
 * @date: 2022/10/24
 */
@Service
public class FlowDesignServiceImpl extends ServiceImpl<FlowDesignMapper, FlowDesignEntity> implements IFlowDesignService {
    @Autowired
    private FlowDesignMapper flowDesignMapper;

    @Resource
    private MmxFlowDefinitionService flowDefinitionService;

    @Override
    public IPage<FlowDesignVO> listFlowDesignPage(FlowDesignQuery flowDesignQuery) {
        IPage<FlowDesignVO> iPage = flowDesignMapper.listFlowDesignPage(flowDesignQuery.getPage(),flowDesignQuery);
        return iPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertFlowDesign(FlowDesignVO flowDesignVO) {
        FlowDesignEntity entity = Convert.convert(FlowDesignEntity.class, flowDesignVO);
        flowDesignMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateFlowDesign(FlowDesignVO flowDesignVO) {
        FlowDesignEntity entity = Convert.convert(FlowDesignEntity.class, flowDesignVO);
         flowDesignMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteFlowDesignByDesignId(Long designId) {
        flowDesignMapper.deleteById(designId);
    }

    @Override
    public FlowDesignVO selectFlowDesignByDesignId(Long designId) {
        FlowDesignEntity entity = flowDesignMapper.selectById(designId);
        FlowDesignVO vo = Convert.convert(FlowDesignVO.class,entity);
        return vo;
    }

    //@Transactional(rollbackFor = Exception.class)
    @Override
    public void deployFlowDesign(FlowDesignDeployVO flowDesignDeployVO) {
        //流程部署
        MmxFlowDefinitionEntity deploy = flowDefinitionService.deploy(flowDesignDeployVO.getDesignContent());
        //获取流程版本
        LambdaUpdateWrapper<FlowDesignEntity> wrapper = new LambdaUpdateWrapper();
        wrapper.set(FlowDesignEntity::getDefinitionId,deploy.getDefinitionId());
        wrapper.set(FlowDesignEntity::getDesignVersion,deploy.getVersion());
        wrapper.eq(FlowDesignEntity::getDesignId,flowDesignDeployVO.getDesignId());
        flowDesignMapper.update(new FlowDesignEntity(),wrapper);

    }


}
